package com.joy.eduservice.service.impl;

import com.joy.eduservice.entity.EduCourse;
import com.joy.eduservice.entity.EduCourseDescription;
import com.joy.eduservice.entity.vo.CourseInfoVo;
import com.joy.eduservice.entity.vo.CoursePublishVo;
import com.joy.eduservice.mapper.EduCourseMapper;
import com.joy.eduservice.service.EduChapterService;
import com.joy.eduservice.service.EduCourseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.joy.servicebase.exceptionHandler.GuliException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PostMapping;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2021-12-02
 */
@Service
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse> implements EduCourseService {

    @Autowired
    private EduCourseDescriptionServiceImpl eduCourseDescriptionService;
    @Autowired
    private EduChapterService eduChapterService;
    @Autowired
    private EduVideoServiceImpl eduVideoService;

    //添加课程信息的方法
    @Override
    public void saveCourseInfo(CourseInfoVo courseInfoVo) {
        //1、向课程表中添加课程基本信息
        //CourseInfoVO对象里面包含的信息是两个表的，所有先取出信息封装成一个表
        EduCourse eduCourse = new EduCourse();
        //将表中courseInfoVo中的对象拿出来，封装到eduCourse中
        BeanUtils.copyProperties(courseInfoVo,eduCourse);
        //添加课程课程基本信息的方法
        int insert = baseMapper.insert(eduCourse);
        if(insert <=0){
            throw new GuliException(20001,"添加课程信息失败");
        }

        //因为课程与描述关系是一对一，所以添加的描述信息的id值必须等于课程的id
        //获取课程信息id
        String id = eduCourse.getId();

        //2、向课程简介表添加课程简介
        //在这个业务层添加另一个业务层的方法，需要注入
        EduCourseDescription eduCourseDescription = new EduCourseDescription();
        BeanUtils.copyProperties(courseInfoVo,eduCourseDescription);
        //设置描述id就是课程id，需要改描述的id生成策略
        eduCourseDescription.setId(id);

        eduCourseDescriptionService.save(eduCourseDescription);
    }

    //根据课程的所有id查询课程的所有信息
    @Override
    public CourseInfoVo getCourseInfo(String courseId) {

        CourseInfoVo courseInfoVo = new CourseInfoVo();

        //查询课程表
        EduCourse eduCourse = baseMapper.selectById(courseId);

        BeanUtils.copyProperties(eduCourse,courseInfoVo);

        //查询描述表
        EduCourseDescription description = eduCourseDescriptionService.getById(courseId);
        courseInfoVo.setDescription(description.getDescription());

        return courseInfoVo;
    }

    @Override
    public void updateCourseInfo(CourseInfoVo courseInfoVo) {
        //修改课程表
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(courseInfoVo,eduCourse);
        int i = baseMapper.updateById(eduCourse);
        if(i<=0){
            throw new GuliException(20001,"更新课程信息失败");
        }

        //修改描述表
        EduCourseDescription description = new EduCourseDescription();
        BeanUtils.copyProperties(courseInfoVo,description);
        eduCourseDescriptionService.updateById(description);
    }

    //课程的最终发布
    //修改课程的状态
    @Override
    public CoursePublishVo publishCourseInfo(String id) {
        CoursePublishVo publishCourseInfo = baseMapper.getPublishCourseInfo(id);

        return publishCourseInfo;
    }

    //删除课程
    @Override
    public void removeCourse(String id) {
        //1、根据课程id删除小结

        eduVideoService.removeVideoByCourseId(id);
        //2、根据课程id删除章节

        eduChapterService.removeChapterByCourseId(id);
        //3、根据课程id删除描述
        //因为和课程是一对一关系，不用重新定义方法，直接删除即可
        eduCourseDescriptionService.removeById(id);
        //4、根据课程id删除课程本身

        int i = baseMapper.deleteById(id);
        if(i<=0){
            throw new GuliException(20001,"删除失败");
        }

    }
}
